<?php
namespace App\Services\System;

use App\Common\Utils\Helper;
use App\Models\Teachers;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\System\Tasks;
use App\Models\System\Rolepower;

class TaskService
{

    /**
     * 获取列表
     * @author lxy
     * @datetime 2023-03-28
     */
    public function getTaskList($request)
    {
        $limit = $request['limit'] ?? 10;
        $where = [];

        $user_id = auth()->user()->id;
        $role_id = auth()->user()->cur_role_id;

        $rolepower = Rolepower::where('role_id',$role_id)->pluck('actionid')->toArray();
        $rolepower = "'".implode("','",$rolepower )."'";
 
        !empty($request['search']['type'])     && $where[] = ['type','=',$request['search']['type']];
        !empty($request['search']['status'])     && $where[] = ['status','=',$request['search']['status']];

        if (isset($request['search']['name']) && !empty($request['search']['name'])){
            $keyword = Helper::check_param($request['search']['name']);
            $where[] = [DB::raw("(name like '%{$keyword}%')"),'1'];
        }

        
        //非管理员只能看到自己参与,审核，驳回的任务
        if(!in_array($role_id,[1])){
            $where[] = [DB::raw(" (type=1 and menu_id in ($rolepower) || (type=2 and c_user_id = '$user_id') || (type=3 and c_user_id = '$user_id') ) "),1];
        }
 
        $result = Tasks::where($where)
                ->orderBy('status','asc')
                ->orderBy('created_at','desc')
                ->paginate($limit)
                ->toArray();
          
        $result['limit'] = $limit;
  
        return $result;
    }

}